package com.atguigu.controller;



import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseControlle;
import com.atguigu.entity.Role;
import com.atguigu.service.PermissionService;
import com.atguigu.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;


import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseControlle {

    @Reference
    private RoleService roleService;

    @Reference
    private PermissionService permissionService;

//    @RequestMapping
//    public String findAll(Model model){
//        List<Role> list = roleService.findAll();
//
//        model.addAttribute("list", list);
//        return "role/index";
//    }

    @RequestMapping
    @PreAuthorize("hasAuthority('role.show')")
    public String findPage(ModelMap model, HttpServletRequest request) {
//        List<Role> list = roleService.findAll();


        Map<String, Object> filters = getFilters(request);
        PageInfo<Role> page = roleService.findPage(filters);

//        model.addAttribute("list", list);
        model.addAttribute("page", page);
        model.addAttribute("filters", filters);
        return "role/index";
    }

    @RequestMapping("/create")
    @PreAuthorize("hasAuthority('role.create')")
    public String create() {
        return "role/create";
    }

    @RequestMapping("/save")
    @PreAuthorize("hasAuthority('role.create')")
    public String save(Role role) {
        roleService.insert(role);
        return "common/successPage";
    }

    @RequestMapping("/edit/{id}")
    @PreAuthorize("hasAuthority('role.edit')")
    public String edit(@PathVariable Long id, Model model) {
        Role role = this.roleService.getById(id);
        model.addAttribute("role", role);
        return "role/edit";
    }

    @PostMapping("/update")
    @PreAuthorize("hasAuthority('role.edit')")
    public String update(Role role) {
        this.roleService.update(role);
        return "common/successPage";
    }

    @GetMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('role.delete')")
    public String delete(@PathVariable Long id) {
        this.roleService.delete(id);
        return "redirect:/role";
    }

    @GetMapping("/assignShow/{roleId}")
    @PreAuthorize("hasAuthority('role.assgin')")
    public String assignShow(ModelMap model, @PathVariable Long roleId) {
        List<Map<String, Object>> zNodes = permissionService.findPermissionsByRoleId(roleId);
        model.addAttribute("zNodes", zNodes);
        model.addAttribute("roleId", roleId);
        return "role/assignShow";
    }

    @PostMapping("/assignPermission")
    @PreAuthorize("hasAuthority('role.assgin')")
    public String assignPermission(Long roleId,Long[] permissionIds){
        permissionService.saveRolePermissionRealtionShip(roleId, permissionIds);
        return "common/successPage";
    }
}
